function ajaxLoader() {
    $("#myModal").modal('hide');
    $('#ajaxLoader').modal({
        backdrop: 'static',
        keyboard: false
    });
}
function ajaxLoaderClose() {
    $('#ajaxLoader').modal('hide');
}
$(document).ready(function() {
    $('form.ajaxForm').on({
        submit: function() {
            ajaxLoader();
            var url = $(this).attr('action');
            var data = $(this).serialize();
            $.ajax({
                type: "POST",
                url: url,
                dataType: 'json',
                enctype: 'multipart/form-data',
                data: data,
                success: function(data) {
                    
                    window[data.fn](data);
                }
            });
            return false;
        }
    });

    $.fn.ocultarDesocultar = function(options){
        $.fn.ocultarDesocultar.defaults={
            preid : '#toggle-',
            input : 'radio'
        };
        var opts = $.extend({}, $.fn.ocultarDesocultar.defaults, options);
        var $anterior="";
        $(this).hide();
        $("input:"+opts.input).on("click",function(){
            $(opts.preid+$anterior).hide();
            var $id= $(this).attr('id');
            $(opts.preid+$id).show();
            $anterior=$id;
        });
    };

    $.fn.comboDinamico = function(options){
        $combo = $(this);
        $.fn.comboDinamico.defaults={
            validar: "estado",
            validar_function: "cargar_estado",
            id_change : 'municipio',
            function_change: 'cargar_municipio',
            modulo: 'visita/'

        };
        var opts = $.extend({}, $.fn.comboDinamico.defaults, options);
    //Cargar
    if($combo.attr("id") == opts.validar){
        ajax(opts.modulo+opts.validar_function,$combo,opts.validar);

        $combo.on('change',function(){
            ajax(opts.modulo+opts.function_change,$(this),opts.id_change);
        });
    }else{
        $combo.on('change',function(){
            ajax(opts.modulo+opts.function_change,$(this),opts.id_change);
        });
    }

    function ajax(url,$cmb,id_return){
        $.ajax({
            type:"POST",
            dataType:"html",
            url: PATH_NAV+url,
            data: {id: $cmb.val()},
            success: function(html){
                if(html != null){
                    $("#"+id_return).append(html);                                      
                }
            }
        });
    }
    };
});
$(function() {
    /*$('.linkAjax').click(function(e) {
        $(".ajaxLoader img").removeClass('hidden');
        href = $(this).attr("href");
        loadContent(href);
        history.pushState('', 'New URL: '+href, href);
        e.preventDefault();
    });
    window.onpopstate = function(event) {
        $(".ajaxLoader img").removeClass('hidden');
        console.log("pathname: "+location.pathname);
        loadContent(location.pathname);
    };*/
});
function loadContent(url){
    $.ajax({
        type: "POST",
        url: url,
        dataType: 'html',
        enctype: 'multipart/form-data',
        data: { method: "ajax", other: "=)" },
    success: function(data) {
        $(".ajaxLoader img").addClass('hidden');
        $('#contentDATA').html(data);
    }
});
    $('li').removeClass('current');
    $('a[href="'+url+'"]').parent().addClass('current');
}
bootstrap_alert=function(id,mensaje){
    $("#"+id).html('<div class="alert fade in"><button type="button" class="close" data-dismiss="alert">&times;</button>'+mensaje+'</div>');
};